#include "sockethandler.h"
#include <QDebug>
#include "../MeetingServer/messagepackage.h"

socketHandler* socketHandler::instance = nullptr;

socketHandler::socketHandler(QObject *parent) : QObject(parent)
{
    socket = new QTcpSocket(this);
    connect(socket,&QTcpSocket::readyRead,this,&socketHandler::onReadyRead);
}

socketHandler::~socketHandler()
{
    if(socket->state() == QAbstractSocket::ConnectedState)
    {
        socket->disconnectFromHost();
    }
    delete socket;
}

QTcpSocket *socketHandler::getSocket() const
{
    return socket;
}

bool socketHandler::connectServe(QString ip,int port)
{
    socket->connectToHost(ip,port);
    if(!socket->waitForConnected(1000))
    {
        qDebug() <<"连接失败";
        return false;
    }
    return true;
}

void socketHandler::onReadyRead()
{
    while (socket->bytesAvailable() > 0)
    {
        //读取
        messagePackage pack = messagePackage::recvMessage(socket);
        qDebug() << "type : " << pack.type();
        switch (pack.type())
        {
        case messagePackage::Register:
            emit regState(pack.ret());
            break;
        case messagePackage::Login:
            emit logState(pack.retInt());
            break;
        case messagePackage::OnlineUser:
            emit onlineUsers(pack.OnlinUsers());
            break;
        case messagePackage::PrivateChat:
            emit privateChat(pack);
            break;
        case messagePackage::AllUsers:
            emit allUsers(pack.allUsers());
            break;
        case messagePackage::Group:
            emit createGroupState(pack);
            break;
        case messagePackage::GroupChat:
            emit groupChat(pack);
            break;
        case messagePackage::InviteGroup:
            emit addGroup(pack.groupName());
            break;
        case messagePackage::UploadFile:
            emit upload(pack.ret());
            break;
        case messagePackage::AllFile:
            emit allFile(pack.allFile());
            break;
        case messagePackage::DownloadFile:
            emit downloadFile(pack);
            break;
        case messagePackage::GroupFile:
            emit groupFile(pack);
            break;
        case messagePackage::Meeting:
            emit retMeeting(pack);
            break;
        case messagePackage::InviteMeeting:
            emit inviteMeeting(pack);
            break;
        case messagePackage::UpdateMeeting:
            emit updateMeeting(pack);
            break;
        case messagePackage::QuitMeetintg:
            break;
        case messagePackage::ClearMeeting:
            emit clearMeeting();
            break;
        }
    }
}


socketHandler* socketHandler::getInstance()
{
    if(instance == nullptr)
    {
        instance = new socketHandler;
    }
    return instance;
}

void socketHandler::removeInstance()
{
    if(instance!=nullptr)
    {
        delete instance;
        instance = nullptr;
    }
}

